package Models

import (
	"fmt"
	"gopkg.in/ini.v1"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"os"
)

var DB *gorm.DB
var err error

func init() {
	config, err := ini.Load("./conf/config.ini")
	if err != nil {
		fmt.Printf("获取配置文件失败，%v\n", err)
		os.Exit(1)
	}
	ip := config.Section("mysql").Key("ip").String()
	port := config.Section("mysql").Key("port").String()
	user := config.Section("mysql").Key("user").String()
	password := config.Section("mysql").Key("password").String()
	database := config.Section("mysql").Key("database").String()
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", user, password, ip, port, database)
	fmt.Println("dsn:", dsn)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		QueryFields: true,
		//SkipDefaultTransaction: true,
	})
	if err != nil {
		fmt.Printf("数据库连接异常%v\n", err.Error())
	}
}
